package com.yymmarket.forum.admin;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yymmarket.util.db.DbOperation;

public class AdminDAO {

	private static final String ADD_ADMIN = "insert into forum_admin values(?,?,?,?,getDate(),?,?,?,?,getDate(),?)";
	
	public boolean addAdmin(AdminBean bean) {
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		if(!db.prepareStatement(ADD_ADMIN)) {
			db.release();
			return false;
		}
		PreparedStatement ps = db.getPStmt();
		try {
			ps.setInt(1, bean.getUserId());
			ps.setInt(2, bean.getForeignerId());
			ps.setInt(3, bean.getAdminLevel());
			ps.setInt(4, bean.getType());
			ps.setLong(5, bean.getEndTime());
			ps.setInt(6, bean.getActionUserId());
			ps.setInt(7, bean.getDeleteMark());
			ps.setInt(8, bean.getDeleteUserId());
			ps.setString(9, bean.getDescription());
			ps.executeUpdate();
			bean.setId(db.getLastInserId("forum_admin"));
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			db.release();
		}
		return true;
	}
	
	private static final String DELETE_ADMIN_BY_ID_DELETEUSERID = "update forum_admin set delete_mark = 1,delete_user_id = ?,delete_time=getDate() where id = ?";
	
	public boolean deleteAdminByIdAndDeleteUserId(int id,int deleteUserId) {
		
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		if(!db.prepareStatement(DELETE_ADMIN_BY_ID_DELETEUSERID)) {
			db.release();
			return false;
		}
		PreparedStatement ps = db.getPStmt();
		try {
			ps.setInt(1, deleteUserId);
			ps.setInt(2, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			db.release();
		}
		
		return true;
	}
	
	private static final String DELETE_ADMIN_BY_ID = "delete from forum_admin where id = ?";
	
	public boolean deleteAdminById(int id) {
		
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		if(!db.prepareStatement(DELETE_ADMIN_BY_ID)) {
			db.release();
			return false;
		}
		PreparedStatement ps = db.getPStmt();
		try {
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			db.release();
		}
		
		return true;
	}
	
	private AdminBean getAdmin(ResultSet rs) throws SQLException{
		AdminBean bean = new AdminBean();
		bean.setId(rs.getInt("id"));
		bean.setUserId(rs.getInt("user_id"));
		bean.setAdminLevel(rs.getInt("admin_level"));
		bean.setType(rs.getInt("type"));
		bean.setCreateTime(rs.getTimestamp("create_time"));
		bean.setEndTime(rs.getLong("end_time"));
		bean.setActionUserId(rs.getInt("action_user_id"));
		bean.setDeleteMark(rs.getInt("delete_mark"));
		bean.setDeleteUserId(rs.getInt("delete_user_id"));
		bean.setDeleteTime(rs.getTimestamp("delete_time"));
		bean.setDescription(rs.getString("description"));
		
		return bean;
	}
	
	private static final String GET_ADMIN_BY_ID = "select * from forum_admin where id = ?";
	
	public AdminBean getAdmin(int id) {
		DbOperation db = new DbOperation(DbOperation.DB_WAP);

		if (!db.prepareStatement(GET_ADMIN_BY_ID)) {
			db.release();
			return null;
		}
		try {

			db.getPStmt().setInt(1, id);
			ResultSet rs = db.getPStmt().executeQuery();
			if (rs.next()) {
				return getAdmin(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			db.release();
		}

		return null;
	}
	
	private static final String GET_ADMIN_BY_TYPE_FOREIGNERID = "select id from forum_admin where type = ? and foreigner_id = ? and delete_mark = 0";
	
	public List<Integer> getAdmin(int type,int foreignerId) {
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		List<Integer> list = new ArrayList<Integer>();
		if (!db.prepareStatement(GET_ADMIN_BY_TYPE_FOREIGNERID)) {
			db.release();
			return list;
		}
		try {

			db.getPStmt().setInt(1, type);
			db.getPStmt().setInt(2, foreignerId);
			ResultSet rs = db.getPStmt().executeQuery();
			while (rs.next()) {
				list.add(rs.getInt(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		} finally {
			db.release();
		}

		return list;
	}
	
	public static void main(String[] args) {
		AdminDAO dao = new AdminDAO();
		AdminBean bean = dao.getAdmin(1);
		System.out.println(bean.getId());
//		AdminBean bean = new AdminBean();
//		bean.setAdminLevel(5);
//		bean.setForeignerId(4);
//		bean.setType(11);
//		bean.setUserId(21);
//		dao.addAdmin(bean);
//		System.out.println(bean.getId());
	}
}
